const tipConfig = [{
      type: "#mpw-tips_topleft",
      className: "tips-bg top-left mpw-tips-before",
      utype: "topLeft",
   },
   {
      type: "#mpw-tips_topmiddle",
      className: "tips-bg top-middle mpw-tips-before",
      utype: "topMiddle",
   },
   {
      type: "#mpw-tips_topright",
      className: "tips-bg top-right mpw-tips-before",
      utype: "topRight",
   },
]
let timeout = null

Component({
   /**
    * 组件的属性列表
    */
   properties: {},

   /**
    * 组件的初始数据
    */
   data: {
      opt: {
         type: "",
         className: "",
         texts: "",
         show:false,
         color:"",
         duration:0,
      }
   },

   /**
    * 组件的方法列表
    */
   methods: {
      show(opt) {
         let that = this;
         console.log(this.data.opt)
         if (this.data.opt.show){
            this.data.opt.show = false;
            this.data.opt.className = "";
            setDta();
         }
         else{
            this.data.opt.show = true;
            for (var a = 0; a < tipConfig.length; a++) {
               for (const key in tipConfig[a]) {
                  if (tipConfig[a].utype == opt.type) {
                     this.data.opt.type = opt.type;
                     this.data.opt.className = tipConfig[a].className;
                     this.data.opt.texts = opt.texts;
                     this.data.opt.color = opt.color;
                     this.data.opt.duration = opt.duration;
                     setDta();
                     return;
                  }
               }
            }
         }
         function setDta(){
            console.log(that.data.opt)
            that.setData({
               opt: that.data.opt
            })
            if (that.data.opt.duration && that.data.opt.duration > 0) {
               if (timeout) {
                  clearInterval(timeout);
                  timeout = null
               }
               timeout = setInterval(() => {
                  that.hide();
               }, that.data.opt.duration)
            }
         }
      },
      hide() {
         this.data.opt.show = false;
         this.data.opt.className = '';
         this.setData({
            opt: this.data.opt
         })
         clearInterval(timeout)
      },

      /**
       * 分配回调
       * @param {String} mpw->组件id
       * @param {String} funt->回调类型
       */
      setCallback(mpw, funt) {
         console.log(this.data)
         for (var a = 0; a < tipConfig.length; a++) {
            for (const key in tipConfig[a]) {
               if (tipConfig[a].type == mpw) {
                  if (funt == 'fail') this.data.opt.fail();
                  if (funt == 'success') this.data.opt.success();
                  return;
               }
            }
         }
      },

      /**
       * @事件绑定 
       * @组件id：#mpw-dialog_default（默认对话框）
       * 取消按钮点击事件
       */
      mpwDialogDefauleConcel() {
         console.log('组件内点击取消')
         this.hide();
         this.setCallback('#mpw-dialog_default', 'fail');
      },

      /**
       * @事件绑定 
       * @组件id：#mpw-dialog_default（默认对话框）
       * 确定按钮点击事件
       */
      mpwDialogDefaultConfirm() {
         console.log('组件内点击确定')
         this.hide();
         this.setCallback('#mpw-dialog_default', 'success');
      },

      /**
       * @事件绑定 
       * @组件id：#mpw-dialog_terms（条款对话框）
       * 关闭按钮点击事件
       */
      mpwDialogTermsClose() {
         this.hide();
         this.setCallback('#mpw-dialog_terms', 'fail');
      },

      /**
       * @事件绑定 
       * @组件id：#mpw-dialog_terms（条款对话框）
       * 确定按钮点击事件
       */
      mpwDialogTermsConfirm() {
         console.log('dialog确定按钮点击事件')
         this.hide();
         this.setCallback('#mpw-dialog_terms', 'success');
      },

      /**
       * @事件绑定 
       * @组件id：#mpw-dialog_timing（即时对话框）
       * 确定按钮点击事件
       */
      mpwDialogTimingConfirm() {
         console.log('dialog确定按钮点击事件')
         this.hide();
         this.setCallback('#mpw-dialog_timing', 'success');
      },
   }
})